<template>
  <div class="check-object">
    <div class="shade"></div>
    <div class="popup">
      <div class="title justify-content-between">
        <span class="fs28 colorFFF">{{title}}</span>
        <img src="../assets/images/icon-close.png" @click="cancel" />
      </div>
      <div class="content">
        <div class="c-title justify-content-between">
          <span class="fs24 color161D2E">检查项目：{{billName}}</span>
          <div  class="btn" @click="listAdd"> 
            <!-- v-if="modelType=='SBQT'" -->
            <img src="../assets/images/icon-add-small.png" />
            <span>增加</span>
          </div>
        </div>
        <div class="theader justify-content-between mt0">
          <div class="w330">序号</div>
          <div class="w1600">开始测试</div>
          <div class="w1600">检查依据</div>
          <div class="w1600">检查结果</div>
          <div class="w1600">备注</div>
          <div class="w700">操作</div>
          <div class="w1600">报告</div>
        </div>
        <div class="tbodys">
          <div
            class="tbody justify-content-between"
            v-for="(item, index) in listData"
            :key="index"
          >
            <div class="w330">{{ index + 1 }}</div>
            <div class="w1600">
              <el-input
                v-model="item.testInfo"
                v-if="item.editing"
                placeholder="请输入开始测试"
              ></el-input>
              <el-tooltip v-else placement="bottom" effect="light">
                <div slot="content" class="fs18">{{ item.testInfo }}</div>
                <div class="text-overflow-2 cursor-pointer">
                  {{ item.testInfo }}
                </div>
              </el-tooltip>
            </div>
            <div class="w1600">
              <el-input
                v-model="item.judge"
                v-if="item.editing"
                placeholder="请输入检查依据"
              ></el-input>
              <el-tooltip v-else placement="bottom" effect="light">
                <div slot="content" class="fs18">{{ item.judge }}</div>
                <div class="text-overflow-2 cursor-pointer">
                  {{ item.judge }}
                </div>
              </el-tooltip>
            </div>
            <div class="w1600 justify-content-between flex-wrap">
              <div
                class="btn"
                :class="item.qualify == 'Y' ? 'active' : ''"
                @click="changeStatus(1, index, item.qualify)"
              >
                是否合格
              </div>
              <div
                class="btn"
                :class="item.data == 'Y' ? 'active' : ''"
                @click="changeStatus(2, index, item.data)"
              >
                数据
              </div>
              <div
                class="btn mt5"
                :class="item.factor == 'Y' ? 'active' : ''"
                @click="changeStatus(3, index, item.factor)"
              >
                试验条件
              </div>
              <div
                class="btn mt5"
                :class="item.media == 'Y' ? 'active' : ''"
                @click="changeStatus(4, index, item.media)"
              >
                多媒体
              </div>
            </div>
            <div class="w1600">
              <el-input
                v-model="item.remark"
                v-if="item.editing"
                placeholder="请输入备注"
              ></el-input>
              <el-tooltip v-else placement="bottom" effect="light">
                <div slot="content" class="fs18">{{ item.remark }}</div>
                <div class="text-overflow-2 cursor-pointer">
                  {{ item.remark }}
                </div>
              </el-tooltip>
            </div>
            <div class="w700 justify-content-between">
              <div class="edit" @click="listEdit(index)"></div>
              <div class="del" @click="listDel(index, item.id)"></div>
            </div>
            <div class="w1600 justify-content-center">
              <el-checkbox v-model="item.report">生成报告</el-checkbox>
            </div>
          </div>
        </div>
      </div>
      <div class="btns justify-content-center">
        <div class="btn sure" @click="save">确认</div>
        <div class="btn cancel" @click="cancel">取消</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "CheckObject",
  props: {
    taskId: Number,
    billId: Number,
    billName: String,
    modelType: String,
    testType: String,
  },
  data() {
    return {
      iconEditShow: require("../assets/images/icon-edit-gray.png"),
      iconEditActive: require("../assets/images/icon-edit-green.png"),
      iconEdit: require("../assets/images/icon-edit-gray.png"),
      iconDelShow: require("../assets/images/icon-del-small-gray.png"),
      iconDel: require("../assets/images/icon-del-small-gray.png"),
      iconDelActive: require("../assets/images/icon-del-small.png"),
      testTypeFormatter: {
        KSCS: '开始测试',
        WGJC: '外观检查',
        SJZL: '随机资料',
        SJFJ: '随机附件'
      },
      listData: [
        {
          testInfo: "",
          judge: "",
          qualify: "N",
          data: "N",
          factor: "N",
          media: "N",
          remark: "",
          report: false,
          editing: true,
        },
      ],
    };
  },
  computed: {
    title: function() {
      return this.testTypeFormatter[this.testType]
    }
  },
  created() {
    this.getListData()
  },
  methods: {
    // 关闭弹窗
    cancel() {
      this.$emit("cancel");
    },
    getListData(type) {
      let _this = this;
      let params = {
        billId: _this.billId,
        modelType: _this.modelType,
        testType: _this.testType,
      };
      _this.loading = true;
      _this.axios
        .getProjectTest(params)
        .then((res) => {
          console.log(res);
          if (res.code == 200) {
            res.data.list.map(item => {
              item.report = item.report == "N" ? false : true;
              return item
            })
            _this.listData = res.data.list
            if(type=="editing"){
               _this.listData[_this.listData.length-1].editing=true
            }
          }
          _this.loading = false;
        })
        .catch(function (error) {
          _this.loading = false;
          _this.$message.error(error.msg);
        });
    },
    // 新增
    listAdd() {
      let _this = this;
      let item = {
        testInfo: "",
        judge: "",
        qualify: "N",
        data: "N",
        factor: "N",
        media: "N",
        remark: "",
        report: false,
        editing: true,
        billId: _this.billId,
        taskId: _this.taskId,
        modelType: _this.modelType,
        testType: _this.testType
      };
      _this.listData.push(item);
      // _this.axios.saveCheckItem({
      //   list: [item]
      // }).then(res => {
      //   console.log(res)
      //   if (res.code == 200) {
      //     _this.getListData("editing")
      //   }
      // }).catch(function (error) {
      //   _this.$message.error(error.msg);
      // });
    },
    /**
     * 检查结果
     * @param {Number} btn - 点击按钮
     * @param {Number} index - 数据下标
     * @param {String} status - 当前状态 Y是 N否
     */
    changeStatus(btn, index, status) {
      let temp = this.listData[index];
      if (btn == 1) {
        temp.qualify = status == "Y" ? "N" : "Y";
      } else if (btn == 2) {
        temp.data = status == "Y" ? "N" : "Y";
      } else if (btn == 3) {
        temp.factor = status == "Y" ? "N" : "Y";
      } else if (btn == 4) {
        temp.media = status == "Y" ? "N" : "Y";
      }
      this.$set(this.listData, index, temp);
    },
    /**
     * 编辑
     * @param {Number} index - 编辑数据下标
     */
    listEdit(index) {
      let temp = this.listData[index];
      temp.editing = true;
      this.$set(this.listData, index, temp);
    },
    /**
     * 删除
     * @param {Number} id - 删除数据id
     */
    listDel(index, id) {
      let _this = this;
      _this.listData.splice(index, 1);
      if (id) {
        _this.axios.delCheckItem({
          id: id
        }).then(res => {
          console.log(res)
          if (res.code == 200) {
            _this.$message({
              message: res.msg,
              type: "success",
            });
            _this.getListData();
          }
        }).catch(function (error) {
          _this.$message.error(error.msg);
        });
      }
    },
    save() {
      let _this = this;
      let params = JSON.parse(JSON.stringify(_this.listData));
      params.map(item => {
        item.report = item.report == false ? "N" : "Y";
        return item
      })
      _this.axios.saveCheckItem({
        list: params
      }).then(res => {
        console.log(res)
        if (res.code == 200) {
          _this.cancel()
        } else {
          _this.$message.error(res.msg)
        }
      }).catch(function (error) {
        _this.$message.error(error.msg);
      });
    }
  },
};
</script>

<style scoped lang='less'>
.check-object {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  .shade {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
  }
  .popup {
    width: 12.43rem;
    height: 7.46rem;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 0 0.05rem 0.15rem 0 rgba(0, 21, 54, 0.25);
    .title {
      width: 100%;
      height: 0.58rem;
      background-color: #285fe3;
      padding: 0 0.25rem 0 0.4rem;
      img {
        width: 0.23rem;
        height: 0.23rem;
        cursor: pointer;
      }
    }
    .content {
      width: 100%;
      padding: 0 0.4rem;
      .c-title {
        width: 100%;
        height: 0.64rem;
        .btn {
          width: 1rem;
          height: 0.34rem;
          display: flex;
          align-items: center;
          justify-content: center;
          border: 1px solid #316eff;
          background: linear-gradient(0deg, #d8dce7 0%, #ffffff 100%);
          border-radius: 0.03rem;
          cursor: pointer;
          img {
            width: 0.14rem;
            height: 0.14rem;
            margin-right: 0.06rem;
          }
          span {
            font-size: 0.18rem;
            color: #285fe3;
          }
        }
      }
      .theader {
        width: 100%;
        height: 0.4rem;
        background: #f4f5f8;
        border: 1px solid #d0d6e3;
        border-radius: 0.03rem;
        padding: 0 0.18rem 0 0.14rem;
        font-size: 0.18rem;
        color: #505971;
        div {
          text-align: center;
        }
      }
      .tbodys {
        width: 100%;
        max-height: 5.04rem;
        overflow-y: scroll;
      }
      .tbodys::-webkit-scrollbar {
        display: none;
      }
      .tbodys {
        scrollbar-width: none;
        -ms-overflow-style: none;
        overflow-x: hidden;
        overflow-y: auto;
      }
      .tbody {
        width: 100%;
        height: 0.78rem;
        border: 1px solid #d0d6e3;
        border-radius: 0.03rem;
        padding: 0 0.18rem 0 0.14rem;
        font-size: 0.18rem;
        color: #161d2e;
        margin-top: 0.05rem;
        .btn {
          width: 49%;
          height: 0.24rem;
          line-height: 0.22rem;
          text-align: center;
          border: 1px solid #316eff;
          border-radius: 0.03rem;
          font-size: 0.18rem;
          color: #285fe3;
          cursor: pointer;
        }
        .btn.active {
          background-color: #285fe3;
          color: #fff;
          border: 0;
        }
        .edit {
          width: 0.25rem;
          height: 0.3rem;
          cursor: pointer;
          background-image: url(../assets/images/icon-edit-gray.png);
          background-size: 100% 100%;
          &:hover {
            background-image: url(../assets/images/icon-edit-green.png);
          }
        }
        .del {
          width: 0.27rem;
          height: 0.3rem;
          cursor: pointer;
          background-image: url(../assets/images/icon-del-small-gray.png);
          background-size: 100% 100%;
          &:hover {
            background-image: url(../assets/images/icon-del-small.png);
          }
        }
      }
      .w330 {
        width: 3.3%;
        text-align: center;
      }
      .w700 {
        width: 7%;
      }
      .w1600 {
        width: 16%;
      }
    }
    .btns {
      width: 100%;
      position: absolute;
      bottom: 0.36rem;
      .btn {
        width: 1.3rem;
        height: 0.4rem;
        margin-left: 1.12rem;
        line-height: 0.4rem;
        text-align: center;
        border-radius: 0.03rem;
        font-size: 0.22rem;
        cursor: pointer;
      }
      .btn.sure {
        background: linear-gradient(0deg, #00a366 0%, #00c37a 100%);
        color: #fff;
      }
      .btn.cancel {
        border: 1px solid #316eff;
        background: linear-gradient(0deg, #d9dde8 0%, #ffffff 100%);
        color: #505971;
      }
      .btn:first-child {
        margin-left: 0;
      }
    }
  }
}
/deep/ .el-checkbox__input.is-checked+.el-checkbox__label {
  color: #161D2E;
}
</style>